<?php

/**
 * catadd.php 添加栏目
 * 
 * @author 老洪
 * @link www.baidu.com
 * @since 2020年11月9日15:23:00
 * @copyright CGX
 */

header("Content-Type:text/html;charset=utf-8");

//判断用户是否登录
// $_SESSION['s_username'] = null;
session_start();
$sessions = empty($_SESSION['s_username']) ? '' : $_SESSION['s_username'];
if ($sessions == '') {
  //用户名或密码不存在，弹出‘该用户不存在’
  echo "<script>alert('请先登录账户！');window.location.href='login.php'</script>";
  exit();
}

require './lib/init.php';

if (empty($_POST)) {
  //如果为空则引入前台栏目添加模板
  require ROOT . '/view/admin/catadd.html';
  exit();
}
$catname = trim($_POST['catname']);
if (empty($catname)) {
  echo "栏目不能为空";
  exit();
}
// var_dump($_POST);
//如果post数据不为空，再判断一下该栏目是否已经存在


$sql = "select * from cat where catname='$catname'";
$res = mQuery($sql);
if (!$res) {
  //记录日志
  echo mysqli_error($conn);
  exit();
}
//获取查询结果
$row = mGetAll($sql);
if ($row) {
  echo "该栏目已存在！";
  exit();
}
//栏目
// $sql = "insert into cat(catname) values('$catname')";
$res = mExec('cat', $_POST);
// $result = mysqli_query($conn, $sql);
if ($res) {
  succ('栏目添加成功！');
}
